package com.szt.school.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Slf4j
public class MyErrorHandler {
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus
    public ModelAndView processException(RuntimeException re){
        log.info("运行时算术");
        ModelAndView mv = new ModelAndView();
        mv.addObject("myException",re.getMessage());
        mv.setViewName("error/500");
        return mv;
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus
    public String processException(Exception e, Model model)
    {
        log.info("自定义异常处理 ‐> Exception!");
        model.addAttribute("myException", e.getMessage());
        return "error/500";
    }

}
